﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Text;
using System.Data;
using System.IO;
using System.Net;
using System.Data.SqlClient;

public partial class DelLotteryAnnouncement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        GetHtmlEx("http://www.17500.cn/getData/ssq.TXT", Encoding.UTF8, 1);

    }

    protected void Button2_Click(object sender, EventArgs e)
    {

        GetHtmlEx("http://www.17500.cn/getData/dlt.TXT", Encoding.UTF8, 2);

    }

    protected void Button3_Click(object sender, EventArgs e)
    {

        GetHtmlEx("http://www.17500.cn/getData/3d.TXT", Encoding.UTF8, 5);
    }
    /// <summary>  
    ///   获取开奖数据
    /// </summary>  
    /// <param name="url">网页地址</param>  
    /// <param name="encoding">编码方式</param>  
    /// <returns></returns>  

    private void GetHtmlEx(string url, Encoding encoding, int type)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.ContentType = "application/x-www-form-urlencoded";
        request.Accept = "*";
        request.Method = "get";
        request.AllowAutoRedirect = false;
        request.Timeout = 50000;

        WebResponse response = request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream, encoding);
        String html = reader.ReadToEnd();//全部读出来
        string filePath = string.Empty;
        string[] allLines;
        if (type == 1)
        {
            filePath = Server.MapPath(@"\App_Data\ssq.txt");
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
            StreamWriter sw = new StreamWriter(fs);
            fs.SetLength(0);//把文件清空了。
            sw.Write(html);//写字符串。
            sw.Close();
            response.Close();
            allLines = File.ReadAllLines(Server.MapPath(@"\App_Data\ssq.txt"));
            int count = 0;
            for (int i = 0; i < allLines.Length; i++)
            {
                string[] newdata = allLines[i].Split(' ');

                GetModel(newdata, type);
                count = count + 1;
            }
            System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('导入成功，共导入" + count + "条信息！');</script>");

        }
        else if (type == 2)
        {
            filePath = Server.MapPath(@"\App_Data\dlt.txt");
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
            StreamWriter sw = new StreamWriter(fs);
            fs.SetLength(0);//把文件清空了。
            sw.Write(html);//写字符串。
            sw.Close();
            response.Close();
            allLines = File.ReadAllLines(Server.MapPath(@"\App_Data\dlt.txt"));
            int count = 0;
            for (int i = 0; i < allLines.Length; i++)
            {
                string[] newdata = allLines[i].Split(' ');

                GetModel(newdata, type);
                count = count + 1;
            }
            System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('导入成功，共导入" + count + "条信息！');</script>");

        }
        else if (type == 3)
        {
            filePath = Server.MapPath(@"\App_Data\p3.txt");
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
            StreamWriter sw = new StreamWriter(fs);
            fs.SetLength(0);//把文件清空了。
            sw.Write(html);//写字符串。
            sw.Close();
            response.Close();
            allLines = File.ReadAllLines(Server.MapPath(@"\App_Data\p3.txt"));
            int count = 0;
            for (int i = 0; i < allLines.Length; i++)
            {
                string[] newdata = allLines[i].Split(' ');

                GetModel(newdata, type);
                count = count + 1;
            }
            System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('导入成功，共导入" + count + "条信息！');</script>");

        }
        else if (type == 4)
        {
            filePath = Server.MapPath(@"\App_Data\p5.txt");
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
            StreamWriter sw = new StreamWriter(fs);
            fs.SetLength(0);//把文件清空了。
            sw.Write(html);//写字符串。
            sw.Close();
            response.Close();
            allLines = File.ReadAllLines(Server.MapPath(@"\App_Data\p5.txt"));
            int count = 0;
            for (int i = 0; i < allLines.Length; i++)
            {
                string[] newdata = allLines[i].Split(' ');

                GetModel(newdata, type);
                count = count + 1;
            }
            System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('导入成功，共导入" + count + "条信息！');</script>");

        }
        else if (type == 5)
        {
            filePath = Server.MapPath(@"\App_Data\3d.txt");
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
            StreamWriter sw = new StreamWriter(fs);
            fs.SetLength(0);//把文件清空了。
            sw.Write(html);//写字符串。
            sw.Close();
            response.Close();
            allLines = File.ReadAllLines(Server.MapPath(@"\App_Data\3d.txt"));
            int count = 0;
            for (int i = 0; i < allLines.Length; i++)
            {
                string[] newdata = allLines[i].Split(' ');

                GetModel(newdata, type);
                count = count + 1;
            }
            System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('导入成功，共导入" + count + "条信息！');</script>");

        }
        else if (type == 6)
        {
            filePath = Server.MapPath(@"\App_Data\7xc.txt");
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
            StreamWriter sw = new StreamWriter(fs);
            fs.SetLength(0);//把文件清空了。
            sw.Write(html);//写字符串。
            sw.Close();
            response.Close();
            allLines = File.ReadAllLines(Server.MapPath(@"\App_Data\7xc.txt"));
            int count = 0;
            for (int i = 1; i < allLines.Length; i++)
            {
                string[] newdata = allLines[i].Split(' ');

                GetModel(newdata, type);
                count = count + 1;
            }
            System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('导入成功，共导入" + count + "条信息！');</script>");

        }
        else if (type == 7)
        {
            filePath = Server.MapPath(@"\App_Data\7lc.txt");
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
            StreamWriter sw = new StreamWriter(fs);
            fs.SetLength(0);//把文件清空了。
            sw.Write(html);//写字符串。
            sw.Close();
            response.Close();
            allLines = File.ReadAllLines(Server.MapPath(@"\App_Data\7lc.txt"));
            int count = 0;
            for (int i = 0; i < allLines.Length; i++)
            {
                string[] newdata = allLines[i].Split(' ');

                GetModel(newdata, type);
                count = count + 1;
            }
            System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('导入成功，共导入" + count + "条信息！');</script>");

        }



    }

    private void GetModel(string[] data, int type)
    {

        if (type == 1)
        {
            #region 双色球
            Model.LotteryAnnouncement model = new Model.LotteryAnnouncement();
            model.AddTime = System.DateTime.Now;
            model.LotteryType = 1;
            model.LotteryQS = data[0];
            model.openTime = DateTime.Parse(data[1]);
            model.LotteryNo = data[2] + "," + data[3] + "," + data[4] + "," + data[5] + "," + data[6] + "," + data[7] + "+" + data[8];
            model.AppearOrderNo = data[9] + "," + data[10] + "," + data[11] + "," + data[12] + "," + data[13] + "," + data[14];
            model.sumMoney = decimal.Parse(data[15]);
            model.jiangChi = decimal.Parse(data[16]);
            model.oneCount = int.Parse(data[17]);
            model.oneMoney = decimal.Parse(data[18]);
            model.twoCount = int.Parse(data[19]);
            model.twoMoney = decimal.Parse(data[20]);
            model.threeCount = int.Parse(data[21]);
            model.threeMoney = decimal.Parse(data[22]);
            model.fourCount = int.Parse(data[23]);
            model.fourMoney = decimal.Parse(data[24]);
            model.fiveCount = int.Parse(data[25]);
            model.fiveMoney = decimal.Parse(data[26]);
            model.sixCount = int.Parse(data[27]);
            model.sixMoney = decimal.Parse(data[28]);
            addWinInfo(model);
            #endregion
        }
        else if (type == 2)
        {
            #region 大乐透
            Model.LotteryAnnouncement model = new Model.LotteryAnnouncement();
            model.AddTime = System.DateTime.Now;
            model.LotteryType = 2;
            model.LotteryQS = data[0];
            model.openTime = DateTime.Parse(data[1]);
            model.LotteryNo = data[2] + "," + data[3] + "," + data[4] + "," + data[5] + "," + data[6] + "+" + data[7] + "," + data[8];
            model.AppearOrderNo = "0";
            model.sumMoney = decimal.Parse(data[9]);
            model.jiangChi = decimal.Parse(data[10]);
            model.oneCount = int.Parse(data[11]);
            model.oneMoney = decimal.Parse(data[12]);
            model.twoCount = int.Parse(data[13]);
            model.twoMoney = decimal.Parse(data[14]);
            model.threeCount = int.Parse(data[15]);
            model.threeMoney = decimal.Parse(data[16]);
            model.fourCount = int.Parse(data[17]);
            model.fourMoney = decimal.Parse(data[18]);
            model.fiveCount = int.Parse(data[19]);
            model.fiveMoney = decimal.Parse(data[20]);
            model.sixCount = int.Parse(data[21]);
            model.sixMoney = decimal.Parse(data[22]);
            addWinInfo(model);
            #endregion
        }
        else if (type == 3)
        {
            #region 排三
            Model.LotteryAnnouncement model = new Model.LotteryAnnouncement();
            model.AddTime = System.DateTime.Now;
            model.LotteryType = 3;
            model.LotteryQS = data[0];
            model.openTime = DateTime.Parse(data[1]);
            model.LotteryNo = data[2] + "," + data[3] + "," + data[4];//开奖号码
            model.AppearOrderNo = "0";
            model.sumMoney = decimal.Parse(data[5]);
            model.jiangChi = 0;
            model.oneCount = int.Parse(data[6]);
            model.oneMoney = decimal.Parse(data[7]);
            model.twoCount = int.Parse(data[8]);
            model.twoMoney = decimal.Parse(data[9]);
            model.threeCount = int.Parse(data[10]);
            model.threeMoney = decimal.Parse(data[11]);
            model.fourCount = 0;
            model.fourMoney = 0;
            model.fiveCount = 0;
            model.fiveMoney = 0;
            model.sixCount = 0;
            model.sixMoney = 0;
            addWinInfo(model);
            #endregion
        }
        else if (type == 4)
        {
            #region 排五
            Model.LotteryAnnouncement model = new Model.LotteryAnnouncement();
            model.AddTime = System.DateTime.Now;
            model.LotteryType = 4;
            model.LotteryQS = data[0];
            model.openTime = DateTime.Parse(data[1]);
            model.LotteryNo = data[2] + "," + data[3] + "," + data[4] + "," + data[5];//开奖号码
            model.AppearOrderNo = "0";
            model.sumMoney = decimal.Parse(data[6]);
            model.jiangChi = 0;
            model.oneCount = int.Parse(data[7]);
            model.oneMoney = decimal.Parse(data[8]);
            model.twoCount = 0;
            model.twoMoney = 0;
            model.threeCount = 0;
            model.threeMoney = 0;
            model.fourCount = 0;
            model.fourMoney = 0;
            model.fiveCount = 0;
            model.fiveMoney = 0;
            model.sixCount = 0;
            model.sixMoney = 0;
            addWinInfo(model);
            #endregion
        }
        else if (type == 5)
        {
            #region 福彩3D
            Model.LotteryAnnouncement model = new Model.LotteryAnnouncement();
            model.AddTime = System.DateTime.Now;
            model.LotteryType = 5;
            model.LotteryQS = data[0];
            model.openTime = DateTime.Parse(data[1]);
            model.LotteryNo = data[2] + "," + data[3] + "," + data[4];//开奖号码
            model.AppearOrderNo = data[5] + "," + data[6] + "," + data[7] + "+" + data[8] + "," + data[9];//试机号+机球
            model.sumMoney = decimal.Parse(data[10]);
            model.jiangChi = 0;
            model.oneCount = int.Parse(data[11]);
            model.oneMoney = decimal.Parse(data[12]);
            model.twoCount = int.Parse(data[13]);
            model.twoMoney = decimal.Parse(data[14]);
            model.threeCount = int.Parse(data[15]);
            model.threeMoney = decimal.Parse(data[16]);
            model.fourCount = 0;
            model.fourMoney = 0;
            model.fiveCount = 0;
            model.fiveMoney = 0;
            model.sixCount = 0;
            model.sixMoney = 0;
            addWinInfo(model);
            #endregion
        }
        else if (type == 6)
        {
            #region 七星彩
            Model.LotteryAnnouncement model = new Model.LotteryAnnouncement();
            model.AddTime = System.DateTime.Now;
            model.LotteryType = 6;
            model.LotteryQS = data[0];
            model.openTime = DateTime.Parse(data[1]);
            model.LotteryNo = data[2] + "," + data[3] + "," + data[4] + "," + data[5] + "," + data[6] + "," + data[7] + "," + data[8];
            model.AppearOrderNo = "0";
            if (data[9].ToString() == "" || data[9].ToString() == null)
            {
                model.sumMoney = 0;
                model.jiangChi = 0;
                model.oneCount = 0;
                model.oneMoney = 0;
                model.twoCount = 0;
                model.twoMoney = 0;
                model.threeCount = 0;
                model.threeMoney = 0;
                model.fourCount = 0;
                model.fourMoney = 0;
                model.fiveCount = 0;
                model.fiveMoney = 0;
                model.sixCount = 0;
                model.sixMoney = 0;
            }
            else
            {
                model.sumMoney = decimal.Parse(data[9]);
                model.jiangChi = decimal.Parse(data[10]);
                model.oneCount = int.Parse(data[11]);
                model.oneMoney = decimal.Parse(data[12]);
                model.twoCount = int.Parse(data[13]);
                model.twoMoney = decimal.Parse(data[14]);
                model.threeCount = int.Parse(data[15]);
                model.threeMoney = decimal.Parse(data[16]);
                model.fourCount = int.Parse(data[17]);
                model.fourMoney = decimal.Parse(data[18]);
                model.fiveCount = int.Parse(data[19]);
                model.fiveMoney = decimal.Parse(data[20]);
                model.sixCount = int.Parse(data[21]);
                model.sixMoney = decimal.Parse(data[22]);
            }
            //if (data[10].ToString() == "" || data[10] == null)
            //{
            //    model.jiangChi = 0;
            //}
            //else
            //{
            //    model.jiangChi = decimal.Parse(data[10]);
            //}
            //if (data[11].ToString() == "" || data[11].ToString() == null)
            //{
            //    model.oneCount = 0;
            //}
            //else
            //{
            //    model.oneCount = int.Parse(data[11]);
            //}
            //if (data[12].ToString() == "" || data[12].ToString() == null)
            //{
            //    model.oneMoney = 0;
            //}
            //else
            //{
            //    model.oneMoney = decimal.Parse(data[12]);
            //}
            //if (data[13].ToString() == "" || data[13].ToString() == null)
            //{
            //    model.twoCount = 0;
            //}
            //else
            //{
            //    model.twoCount = int.Parse(data[13]);
            //}
            //if (data[14].ToString() == "" || data[14].ToString() == null)
            //{
            //    model.twoMoney = 0;
            //}
            //else
            //{
            //    model.twoMoney = decimal.Parse(data[14]);
            //}
            //if (data[15].ToString() == "" || data[15].ToString() == null)
            //{
            //    model.threeCount = 0;
            //}
            //else
            //{
            //    model.threeCount = int.Parse(data[15]);
            //}
            //if (data[16].ToString() == "" || data[16].ToString() == null)
            //{
            //    model.threeMoney = 0;
            //}
            //else
            //{
            //    model.threeMoney = decimal.Parse(data[16]);
            //}
            //if (data[17].ToString() == "" || data[17].ToString() == null)
            //{
            //    model.fourCount = 0;
            //}
            //else
            //{
            //    model.fourCount = int.Parse(data[17]);
            //}
            //if (data[18].ToString() == "" || data[18].ToString() == null)
            //{
            //    model.fourMoney = 0;
            //}
            //else
            //{
            //    model.fourMoney = decimal.Parse(data[18]);
            //}
            //if (data[19].ToString() == "" || data[19].ToString() == null)
            //{
            //    model.fiveCount = 0;
            //}
            //else
            //{
            //    model.fiveCount = int.Parse(data[19]);
            //}
            //if (data[20].ToString() == "" || data[20].ToString() == null)
            //{
            //    model.fiveMoney = 0;
            //}
            //else
            //{
            //    model.fiveMoney = decimal.Parse(data[20]);
            //}
            //if (data[21].ToString() == "" || data[21].ToString() == null)
            //{
            //    model.sixCount = 0;
            //}
            //else
            //{
            //    model.sixCount = int.Parse(data[21]);
            //}
            //if (data[22].ToString() == "" || data[22].ToString() == null)
            //{
            //    model.sixMoney = 0;
            //}
            //else
            //{
            //    model.sixMoney = decimal.Parse(data[22]);
            //}

            addWinInfo(model);
            #endregion
        }
        else if (type == 7)
        {
            #region 七乐彩
            Model.LotteryAnnouncement model = new Model.LotteryAnnouncement();
            model.AddTime = System.DateTime.Now;
            model.LotteryType = 7;
            model.LotteryQS = data[0];
            model.openTime = DateTime.Parse(data[1]);
            model.LotteryNo = data[2] + "," + data[3] + "," + data[4] + "," + data[5] + "," + data[6] + "," + data[7] + "," + data[8] + "+" + data[9];
            model.AppearOrderNo = "0";
            model.sumMoney = decimal.Parse(data[10]);
            model.jiangChi = decimal.Parse(data[11]);
            model.oneCount = int.Parse(data[12]);
            model.oneMoney = decimal.Parse(data[13]);
            model.twoCount = int.Parse(data[14]);
            model.twoMoney = decimal.Parse(data[15]);
            model.threeCount = int.Parse(data[16]);
            model.threeMoney = decimal.Parse(data[17]);
            model.fourCount = int.Parse(data[18]);
            model.fourMoney = decimal.Parse(data[19]);
            model.fiveCount = int.Parse(data[20]);
            model.fiveMoney = decimal.Parse(data[21]);
            model.sixCount = int.Parse(data[22]);
            model.sixMoney = decimal.Parse(data[23]);
            model.sevenCount = int.Parse(data[24]);
            model.sevenMoney = decimal.Parse(data[25]);
            addWinInfo(model);
            #endregion
        }

    }


    private static void addWinInfo(Model.LotteryAnnouncement model)
    {
        string sql = "select top 1 * from LotteryAnnouncement where LotteryType=" + model.LotteryType + " and lotteryQs=" + model.LotteryQS;
        if (BLL.LotteryQs.NewsTopOne(sql) == null)
        {
            BLL.LotteryAnnouncement.Add(model);
        }

    }
    /// <summary>
    /// 清空开奖数据表
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button4_Click(object sender, EventArgs e)
    {
        string sqlDel = "truncate table LotteryAnnouncement";
        DeleteData(sqlDel);
        System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('删除成功！');</script>");
    }

    public int DeleteData(string sql)
    {
        return Data.DbHelp.ExecuteNonQuery(Data.DBConfig.ConnectionSettings, CommandType.Text, sql, null);
    }

    /// <summary>
    /// 删除双色球开奖数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button5_Click(object sender, EventArgs e)
    {
        string strDel = "delete from LotteryAnnouncement where LotteryType=1";
        DeleteData(strDel);
        System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('删除成功！');</script>");
    }
    /// <summary>
    /// 删除大乐透开奖数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button6_Click(object sender, EventArgs e)
    {
        string strDel = "delete from LotteryAnnouncement where LotteryType=2";
        DeleteData(strDel);
        System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('删除成功！');</script>");
    }
    /// <summary>
    /// 删除福彩3D开奖数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button7_Click(object sender, EventArgs e)
    {
        string strDel = "delete from LotteryAnnouncement where LotteryType=5";
        DeleteData(strDel);
        System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('删除成功！');</script>");
    }

    protected void Button8_Click(object sender, EventArgs e)
    {
        string strDel = "delete from LotteryAnnouncement where LotteryType=3";
        DeleteData(strDel);
        System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('删除成功！');</script>");
    }

    protected void Button10_Click(object sender, EventArgs e)
    {
        string strDel = "delete from LotteryAnnouncement where LotteryType=4";
        DeleteData(strDel);
        System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('删除成功！');</script>");
    }

    protected void Button12_Click(object sender, EventArgs e)
    {
        string strDel = "delete from LotteryAnnouncement where LotteryType=6";
        DeleteData(strDel);
        System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('删除成功！');</script>");
    }

    protected void Button14_Click(object sender, EventArgs e)
    {
        string strDel = "delete from LotteryAnnouncement where LotteryType=7";
        DeleteData(strDel);
        System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('删除成功！');</script>");
    }

    protected void Button9_Click(object sender, EventArgs e)
    {
        GetHtmlEx("http://www.17500.cn/getData/p3.TXT", Encoding.UTF8, 3);
    }

    protected void Button11_Click(object sender, EventArgs e)
    {
        GetHtmlEx("http://www.17500.cn/getData/p5.TXT", Encoding.UTF8, 4);
    }

    protected void Button13_Click(object sender, EventArgs e)
    {
        GetHtmlEx("http://www.17500.cn/getData/7xc.TXT", Encoding.UTF8, 6);
    }

    protected void Button15_Click(object sender, EventArgs e)
    {
        GetHtmlEx("http://www.17500.cn/getData/7lc.TXT", Encoding.UTF8, 7);
    }
}